package leetcode.code0962;

import java.util.Stack;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {
	public int maxWidthRamp(int[] nums) {
		int ans = 0;
		Stack<Integer> stack = new Stack<>();
		for (int i = 0; i < nums.length; i++) {
			while (!stack.isEmpty() && nums[stack.peek()] >= nums[i]) {
				stack.pop();
			}
			int cur = stack.isEmpty() ? i : i - stack.peek();
			ans = Math.max(ans, cur);
			stack.add(i);
		}
		int i = stack.pop();
		while (!stack.isEmpty()) {
			ans = Math.max(ans, i - stack.pop());
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(4, this.maxWidthRamp(H.str2array3("[6,0,8,2,1,5]")));
		H.compare(7, this.maxWidthRamp(H.str2array3("[9,8,1,0,1,9,4,0,4,1]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
